<!DOCTYPE html>
<html>
    <head>
        <title>Pick-up Game</title>
        <link rel="stylesheet" href="style.css" type="text/css" />
        <link rel="stylesheet" href="friends.css" type="text/css" />
        <script type="text/javascript" src="friends.js"></script>
		

    </head>

    <body>
		<?php 
		include_once('menu.php');
		if (!empty($_SESSION['username'])) : ?>		
        

        <?php
        // connect to database	
        $conn_str = 'host=dbsrv1.cdf.toronto.edu dbname=csc309g23 user=csc309g23 password=ipheid5h';
        $conn = pg_connect($conn_str);

        // query database
        $query = 'SELECT friend FROM friends WHERE username=\'' . $_SESSION['username'] . '\'';
        $friend_result = pg_query($conn, $query);
        ?>
        <div class="container">            
        <div id="left_friends">		  
			<div style="border-bottom: 1px solid silver;" >
			  <img id="user_image" src="<?php echo $_SESSION['display'] ?>" align='middle' width=180 height=240 style="margin: 10px 10px 10px 10px;"/>							
			</div>		
			<div id="friend_list" style="margin: 0px 0px 0px 0px" >
			  <h2 style="margin: 10px 0px 10px 0px"> Friends List </h2>			
			  <table  border=0>
				  <?php while ($row = pg_fetch_row($friend_result)) { ?>	
					<tr>
					  <td>
						  <div id="friends_info" class="hand" onclick="location.href='message.php?postfriend=<?= $row[0] ?>';" >
								<?php
								  $query = 'SELECT username, firstname, lastname, showfirstname, showlastname, display FROM members WHERE username=\'' . $row[0] . '\'';
								  $friend_info_table = pg_query($conn, $query);
								  $friend_info = pg_fetch_row($friend_info_table);
								?>
										  <table width=190>
											  <tr><td >
													  <table border=0 cellspacing=0 >
														  <tr><td  width=30 height=40>
																  <img src="<?php echo $friend_info[5] ?>" alt="No Image" align='middle' width=30 height=40/>
														  </td>                                                        
														  <td id='friend_info' width=100% > <?php
															if ($friend_info[3] == "t")
															  echo $friend_info[1] . ' ';
															if ($friend_info[4] == "t")
															  echo $friend_info[2]; ?>
															  
														  </td>                                                        
														  </tr>
													  </table>
												  </td>
											  </tr> 								   			
										  </table>
						  </div>						
					  </td>
					</tr>
				  <?php } ?>
			  </table>	
			</div>
		</div>
        
        <div id="right_friends">
		  <div id="user_info">
			<table style="margin: 25px">
			  <?php if (($_SESSION['showfirstname'] == true) OR ($_SESSION['showlastname'] == true )) ?>
				<tr> 	<td align="right" ></td>	
					  <td><b style="font-size:150%;"> <?php
						//if ($_SESSION['showfirstname'] == "t")
						  echo $_SESSION['firstname'] . ' ';
						//if ($_SESSION['showlastname'] == "t" )
						  echo $_SESSION['lastname'] . '</br>' ; ?></b>
				</tr>
							
				<?php if ($_SESSION['showbirthday'] == "t" and $_SESSION['birthday'])  {?>
					<tr> <td align="right" ><!--  <b>Birthday:      </b> --></td>
					  <td><?php 	echo $_SESSION['birthday'] . '</br>';  ?> </td>
					</tr> <?php } ?>	  
							
				<?php if ($_SESSION['showaddress'] == "t" and $_SESSION['address']) { ?>
					<tr> <td align="right" ><!-- <b>Address:      </b> --></td>
					  <td><?php echo $_SESSION['address'] . '</br>'; ?> </td>
					</tr> <?php } ?>
					
				<?php if ($_SESSION['showemail'] == "t" and $_SESSION['email']) { ?>
					<tr> <td align="right" ><!-- <b>Email:      </b> --></td>
					  <td><?php echo $_SESSION['email'] . '</br>';  ?> </td>
					</tr> <?php } ?>						  
			</table>
		  </div>
		  <div id="messages">
			<h2 align="center" style="margin: 10px 0px 10px 0px">Messages</h2>			
							<?php
							$message_query = 'SELECT wall.username, wall.postfriend, wall.postdate, wall.post, members.firstname, members.lastname ' .
											 'FROM wall LEFT JOIN members ON wall.username=members.username ' .
											 'WHERE postfriend=\'' . $_SESSION['username'] . 
											 '\' order by postdate desc';
							$friend_message = pg_query($conn, $message_query);
							while ($message = pg_fetch_row($friend_message)) {
								?>								  	
										  <div class="single_msm" onclick="location.href='message.php?postfriend=<?= $message[0] ?>';">
											<strong style="color:#4682B4;font-size:110%;margin-left:5px;" ><?php echo $message[4] . ' ' . $message[5];?></strong>
											<p style="color:silver;float:right;"> <?	echo(strftime("%b %d, %Y %X %Z", strtotime($message[2])));  ?></p> 											
											<p style="margin-left:10px"><?= $message[3] ?></p>
										  </div>
							<?php } ?> <!-- end while -->
		  </div>
        
        
        </div>
        
		
         
         
         
        
        
        
    <?php
        else :
            header('Location: /');
        endif;
        ?>	
    </div>
    </body>
</html>


